package com.chujian.Activities;

import java.text.SimpleDateFormat;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.chujian.Threads.HttpClientPostThread;
import com.chujian.Utils.Constan;
import com.chujian.Utils.Util;
import com.chujian.model.Book;
import com.chujian.orderingsystem.R;
import com.google.gson.Gson;

public class BookSeatActivity extends Activity{
	private EditText mEtbookTime;
	private EditText mEtTableName;
	private EditText mEtPeopleCount;
	private EditText mEtRemark;
	private String tableName;
	private Long time;
	private Handler mHandler=new Handler(){
		public void handleMessage(android.os.Message msg) {
			super.handleMessage(msg);
			String str=(String) msg.obj;
			if(str.equals("bookFailed")){
				Util.showTextShort(getApplicationContext(), "预订失败");
			}else if(!str.equals("")&&str!=null){
				Util.showTextShort(getApplicationContext(), "预订成功");
				Intent intent =new Intent(getApplicationContext(), BookDetailConsumerActivity.class);
				intent.putExtra("book", str);
				startActivity(intent);
				BookSeatActivity.this.finish();
			}
			
		};
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.book_seat);
		initView();
	}
	
	private void initView() {
		// TODO Auto-generated method stub
		mEtbookTime=(EditText) findViewById(R.id.et_book_seat_time);
		mEtTableName=(EditText) findViewById(R.id.et_book_table_select);
		mEtPeopleCount=(EditText) findViewById(R.id.et_book_people_count);
		mEtRemark=(EditText) findViewById(R.id.et_book_seat_remark);
	}
	@SuppressLint("SimpleDateFormat")
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode==1001&&requestCode==1000){
			time=data.getLongExtra("time", 0l);
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			String dateTime = df.format(new java.util.Date(time));
			mEtbookTime.setText(dateTime);
			
		}
		if(resultCode==1002&&requestCode==1000){
			tableName=data.getStringExtra("tableName");
			mEtTableName.setText(tableName);
		}
	}
	public void doClick(View v){
		switch (v.getId()) {
		case R.id.ib_book_seat_time:
			Intent intent =new Intent(getApplicationContext(), BookTimeActivity.class);
			startActivityForResult(intent, 1000);
			break;
		case R.id.ib_book_table_select:
			Intent intent2 =new Intent(getApplicationContext(), TableSelectActivity.class);
			intent2.putExtra("bookFlag", true);
			startActivityForResult(intent2, 1000);
			break;
		default:
			break;
		}
	}
	public void IbBack(View v){
		finish();
	}

	
	public void submit(View v){
		
		String bookRemark=mEtRemark.getText().toString();
		if(bookRemark.equals("")||mEtPeopleCount.getText().toString().equals("")||tableName.equals("")){
			
			Util.showTextShort(getApplicationContext(), "请将信息填写完整");
			return ;
		}
		int peopleCount=0;
		try {
			peopleCount=Integer.parseInt(mEtPeopleCount.getText().toString());
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Util.showTextShort(getApplicationContext(), "格式错误，请重新输入");
			return ;
		}
//		Toast.makeText(getApplicationContext(), "立即订座", Toast.LENGTH_SHORT).show();
		Book book=new Book(time, peopleCount, tableName, bookRemark, "18630338082");
		Gson gson=new Gson();
		String jsonString=gson.toJson(book);
		
		
		String url=Constan.BASE_URL+"BookSeatServlet";
		new HttpClientPostThread(url, mHandler, jsonString).start();;
		
	}
	
}
